package com.itheima.test;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class Dom4jDemo {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(Dom4jDemo.class.getClassLoader().getResource("student.xml"));
        //获取根元素
        Element rootElement = document.getRootElement();
        //获取子元素
        List elements = rootElement.elements();
        //获取第一个student元素的number属性
        Element element = (Element) elements.get(0);
        String number = element.attributeValue("number");
        System.out.println(number);
        //
        System.out.println("---------");
        Element name = element.element("name");
        Element element1 = (Element) name.elements().get(0);
        System.out.println(element1);
        System.out.println("---------");
        String text = element1.getText();
        System.out.println(text);


    }
}
